package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.*;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired(required = false)
    private OrdersMapper ordersMapper;
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;
    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    @Transactional
    public void add(Orders orders, Long userId) {
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
        User user = userMapper.findUserById(userId);
        Long orderId = new Long(UUIDUtils.getUUIDInOrderId());
        //设置订单里面的属性
        orders.setId(orderId);
        orders.setNumber(orderId + "");
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());

        BigDecimal total = new BigDecimal("0");

        //循环设置订单明细表里面的属性
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAll(userId);
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            Long orderDetailId = new Long(UUIDUtils.getUUIDInOrderId());
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(orderDetailId);
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetailList.add(orderDetail);
            total = total.add(new BigDecimal(orderDetail.getNumber()).multiply(orderDetail.getAmount()));
        }
        //设置订单表总金额
        orders.setAmount(total);
        ordersMapper.save(orders);
        orderDetailMapper.save(orderDetailList);
        shoppingCartMapper.deleteByUserId(userId);
    }

    @Override
    public Page<OrderDto> findOrderAndOrderDetail(Integer page, Integer pageSize, Long uid) {
        PageHelper.startPage(page, pageSize);
        List<Orders> orderList = ordersMapper.findOrdersById(uid);
        PageInfo<Orders> pageInfo = new PageInfo<>(orderList);

        List<OrderDto> orderDtoList = orderList.stream().map((order ->
        {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(order, orderDto);
            List<OrderDetail> orderDetailList = orderDetailMapper.findOrderDetailById(orderDto.getId());
            orderDto.setOrderDetails(orderDetailList);
            return orderDto;
        }
        )).collect(Collectors.toList());
        Page<OrderDto> pageObj = new Page<>();
        pageObj.setTotal(pageInfo.getTotal());
        pageObj.setRecords(orderDtoList);
        pageObj.setPage(pageInfo.getPageNum());
        pageObj.setPageSize(pageInfo.getPageSize());
        return pageObj;
    }
}
